package com.example.pfe;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class MonAutreActivite extends Activity  implements OnClickListener{
	Button monBouton = null;
	TextView result = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_mon_autre_activite);
		monBouton = (Button)findViewById(R.id.button2);
		result= (TextView)findViewById(R.id.result);
		monBouton.setOnClickListener(this);
		String result = null;
		InputStream is = null;
		StringBuilder sb = null;
		ArrayList nameValuePairs = new ArrayList();
		List r = new ArrayList();
		try{

			//http post
			HttpClient httpclient = new DefaultHttpClient();
			HttpGet httpget = new HttpGet("http://10.0.2.2/tableauDeBord.json");			
			HttpResponse response = httpclient.execute(httpget);
			HttpEntity entity = response.getEntity();
			is = entity.getContent();

			
			}
			catch(Exception e){

				Toast.makeText(getBaseContext(),"aaaa" ,Toast.LENGTH_LONG).show();
			Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
			}
		//Convert response to string
		try
		 {
		 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));

		 sb = new StringBuilder();

		 String line = null;

		 while ((line = reader.readLine()) != null)
		 {
		 sb.append(line + "\n");
		 }

		 is.close();

		 result = sb.toString();
		 }
		 catch(Exception e)
		 {
		 Toast.makeText(getBaseContext(),e.toString() ,Toast.LENGTH_LONG).show();
		 }
		//END Convert response to string
		 try{
			 result.toUpperCase();
			 result.concat(result);
			 Toast.makeText(getBaseContext(),result ,Toast.LENGTH_LONG).show(); 
		 JSONObject jArray = new JSONObject(result);
		 JSONObject json_data=null;
		 Toast.makeText(getBaseContext(),jArray.getString("token") ,Toast.LENGTH_LONG).show();
		
		 
		 
		 }
		 catch(JSONException e1){
			
		 Toast.makeText(getBaseContext(),e1.toString() ,Toast.LENGTH_LONG).show();
		 } 

		 
		 }
	

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_mon_autre_activite, menu);
		return true;
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v == monBouton){
			Intent monIntent = new Intent(this,MainActivity.class);
			startActivity(monIntent);
		}
		
	}

}
